/* 监听鼠标移动
    
    外部程序调用后，对外输出鼠标当前绝对坐标 x，y
*/
import { ref, onMounted, onUnmounted } from "vue";

const useMouse = () => {
	const x = ref(0);
	const y = ref(0);

	function Update(event) {
		x.value = event.pageX;
		y.value = event.pageY;
	}

	// 组合式函数可挂靠到所属组件的生命周期钩子上
	// 组件加载后
	onMounted(() => {
		window.addEventListener("mousemove", Update);
	});

	// 组件卸载
	onUnmounted(() => {
		window.removeEventListener("mousemove", Update);
	});

	return {
		x,
		y,
	};
};

export default useMouse;
